const updateUser = "https://ali.ikenway.net:18443/api/user/update";
const UserInfoUrl = "https://ali.ikenway.net:18443/api/user/userInfo";

const picUploadUrl = "https://ali.ikenway.net:18443/api/upload";


let token = sessionStorage.getItem("token");

let userInfo = null;

function getUserInfo() {
    fetch(UserInfoUrl, {
        method:  'GET',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        }
    }).then(res => {
        res.json().then(json => {
            if (json.code === 200) {
                userInfo = json.data;
                initModal();
                initUserInfo();
            } else {
                alertMsg(json.msg);
                if (json.code == 400) {
                    setTimeout(function () {
                        window.location.href = loginPageUrl;
                    }, 2000);
                }
            }
        })
    });
}

function initUserInfo() {
    document.getElementById("avatar").setAttribute("src", userInfo.avatar);
    document.getElementById("name").value = userInfo.name;
    document.getElementById("account").value = userInfo.username;
    document.getElementById("identity").value = userInfo.identity
}

function logout() {
    document.getElementById("alert").hidden = false;
    setTimeout(function () {
        window.location.href = loginPageUrl;
    }, 2000);
}

function userEdit() {
    userInfo.name = document.getElementById("name").value;

    fetch(updateUser, {
        method:  'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        },
        body: JSON.stringify(userInfo)
    }).then(res => {
        res.json().then(json => {
            alertMsg(json.msg);
            if (json.code == 400) {
                setTimeout(function () {
                    window.location.href = loginPageUrl;
                }, 2000);
            }

        });
    })
}

function initModal() {
    let element = document.getElementById("uploadAvatar");
    element.onchange = function () {
        let file = element.files[0];

        let formData = new FormData();

        formData.append("file", file);

        fetch(picUploadUrl, {
            method: 'POST',
            headers: {
                'Authorization': token
            },
            body: formData
        }).then(res => {
            res.json().then(json => {
                console.log(json);

                alertMsg(json.msg);
                if (json.code == 400) {
                    setTimeout(function () {
                        window.location.href = loginPageUrl;
                    }, 2000);
                }

                if (json.code === 200) {
                    document.getElementById("save").onclick = function () {
                        updateUserAvatar(json.data.url);
                    }
                }
            })
        });
    }
    

}

function updateUserAvatar(fileUrl) {
    userInfo.avatar = fileUrl;

    fetch(updateUser, {
        method:  'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        },
        body: JSON.stringify(userInfo)
    }).then(res => {
        res.json().then(json => {
            alertMsg(json.msg);
            if (json.code == 400) {
                setTimeout(function () {
                    window.location.href = loginPageUrl;
                }, 2000);
            }
        });
    })
}


function editUser() {
    userInfo.name = document.getElementById("name").value;
    fetch(updateUser, {
        method:  'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        },
        body: JSON.stringify(userInfo)
    }).then(res => {
        res.json().then(json => {
            alertMsg(json.msg)
        })
    })
}